package com.lcx.db137.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lcx.db137.entity.Role;
import com.lcx.db137.mapper.RoleMapper;
import com.lcx.db137.service.RolemenuService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcx.db137.mapper.RolemenuMapper;
import com.lcx.db137.entity.Rolemenu;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description  
 * @version 1.0 
 * @author lcx
 * @date 2024/12/24 11:48
 */
@Service
public class RolemenuServiceImpl extends ServiceImpl<RolemenuMapper, Rolemenu> implements RolemenuService {



    @Override
    //事务
    @Transactional
    public boolean updateMenuIdByRoleId(int roleid, List<Integer> listMenu) {
        //1.根据角色id删除这个角色的所有菜单
        UpdateWrapper<Rolemenu> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("roleid", roleid);
        baseMapper.delete(updateWrapper);
        //2.根据角色id添加新的菜单
        for (Integer menuid : listMenu){
            Rolemenu rolemenu = new Rolemenu();
            rolemenu.setRoleid(roleid);
            rolemenu.setMenuid(menuid);
            baseMapper.insert(rolemenu);
        }
        return true;
    }
}
